package cn.itzzx.lagou.minicat.request;

import java.io.IOException;
import java.io.InputStream;

/**
 * 把请求信息封装成 Request对象 （根据InputStream输入流封装）
 *
 * @author Martin
 * @since 1.0
 */
public class Request {

    /**
     * 请求方式 get/post
     */
    private String method;

    private String host;

    /**
     * 如 / , /index
     */
    private String url;


    /**
     * 输入流，其他属性从输入流中解析出来
     */
    private InputStream inputStream;

    public Request() {

    }

    //构造器 输入流传入
    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;
        //数据长度 如果为0 让他一直读取
        int count = 0;
        while (count == 0) {
            count = inputStream.available();
        }
        byte[] bytes = new byte[count];
        //将输入流读取到 bytes
        inputStream.read(bytes);
        String inputStr = new String(bytes);

        String firstLine = inputStr.split("\n")[0];

        String[] strings = firstLine.split(" ");

        this.method = strings[0];
        this.url = strings[1];
        System.out.println("method -------> " + method + " url ---------->" + url);
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }
}
